package calao.test.util.xml;

import java.io.Reader;

import calao.util.xml.XmlElement;
import calao.util.xml.XmlParseException;
import calao.util.xml.XmlParser;

public class Config {
	
	static enum Element { GROUP_LIST, GROUP, CONTACT };
	
	static enum Property { FIRST_NAME, LAST_NAME, NAME, DESCRIPTION };

	static class Parser extends XmlParser<Element,Property> {
		Parser(Reader reader) {
			super(reader, Element.class, Property.class);
		}	
		@Override
		protected void handle(XmlElement<Element,Property> element)
		throws java.io.IOException, XmlParseException
		{
			switch (element.getTag()) {
			case GROUP_LIST:
				Group.List groupList = new Group.List();
				for (;;) {
					XmlElement child = parseElement(Element.GROUP);
					if (child==null) break;
					Group group = (Group)child.getValue();
					groupList.add(group);
					element.setValue(groupList);
				}
				break;
			case GROUP:
				Group group = new Group();
				group.name = element.get(Property.NAME);
				group.description = element.get(Property.DESCRIPTION);
				for (;;) {
					XmlElement child = parseElement(Element.CONTACT);
					if (child==null) break;
					Contact contact = (Contact)child.getValue();
					group.add(contact);
					element.setValue(group);
				}
				break;
			case CONTACT:
				Contact contact = new Contact(
						element.get(Property.FIRST_NAME),
						element.get(Property.LAST_NAME));
				element.setValue(contact);
				break;
			default:
				abort();
			}
		}
	}

}
